#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <mqueue.h>

#include "config.h"
#include "balr_archivage.h"


void* BALRarchivage()
{
	// Initialisation -----------------------------------------------
	mqd_t queue_versArchivage;
	int reading_size;
	MSG_versArchivage msg;

	// opening "BAL_versArchivage", READ only
	queue_versArchivage = mq_open(BAL_versArchivage, O_RDONLY);
	if(queue_versArchivage == -1)
	{
		perror("[BALR_Archivage] Error opening BAL_versArchivage:");
		return 1;
	}



	// Running loop -------------------------------------------------

	for(;;)
	{
		// waiting for a msg
		reading_size = mq_receive(queue_versArchivage, (char *) &msg, sizeof(MSG_versArchivage), NULL);
		
		if(reading_size == -1)
		{
			perror("[BAL_Archivage] Failed to receive msg from BAL_versArchivage:");
		}
		else
		{
			// Reading the msg
			int source = msg.source;
			int message = msg.message;
			time_t heure = msg.heure;
			int numLot = msg.numLot;
			int cartonEnCours = msg.cartonEnCours; 
			int type = msg.type;

			printf("[BAL_Archivage] Received archivage msg:\n- source = %d\n- message nb = %d\n\n", source, message);
		}
	}
}
